package Controllers;

import Controllers.exceptions.IllegalOrphanException;
import Controllers.exceptions.NonexistentEntityException;
import Entities.HdTipoUser;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entities.HdUsers;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Ferrari
 */
public class HdTipoUserJpaController implements Serializable {

    public HdTipoUserJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(HdTipoUser hdTipoUser) {
        if (hdTipoUser.getHdUsersList() == null) {
            hdTipoUser.setHdUsersList(new ArrayList<HdUsers>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<HdUsers> attachedHdUsersList = new ArrayList<HdUsers>();
            for (HdUsers hdUsersListHdUsersToAttach : hdTipoUser.getHdUsersList()) {
                hdUsersListHdUsersToAttach = em.getReference(hdUsersListHdUsersToAttach.getClass(), hdUsersListHdUsersToAttach.getCoUser());
                attachedHdUsersList.add(hdUsersListHdUsersToAttach);
            }
            hdTipoUser.setHdUsersList(attachedHdUsersList);
            em.persist(hdTipoUser);
            for (HdUsers hdUsersListHdUsers : hdTipoUser.getHdUsersList()) {
                HdTipoUser oldCoTipoUserOfHdUsersListHdUsers = hdUsersListHdUsers.getCoTipoUser();
                hdUsersListHdUsers.setCoTipoUser(hdTipoUser);
                hdUsersListHdUsers = em.merge(hdUsersListHdUsers);
                if (oldCoTipoUserOfHdUsersListHdUsers != null) {
                    oldCoTipoUserOfHdUsersListHdUsers.getHdUsersList().remove(hdUsersListHdUsers);
                    oldCoTipoUserOfHdUsersListHdUsers = em.merge(oldCoTipoUserOfHdUsersListHdUsers);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(HdTipoUser hdTipoUser) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            HdTipoUser persistentHdTipoUser = em.find(HdTipoUser.class, hdTipoUser.getCoTipoUser());
            List<HdUsers> hdUsersListOld = persistentHdTipoUser.getHdUsersList();
            List<HdUsers> hdUsersListNew = hdTipoUser.getHdUsersList();
            List<String> illegalOrphanMessages = null;
            for (HdUsers hdUsersListOldHdUsers : hdUsersListOld) {
                if (!hdUsersListNew.contains(hdUsersListOldHdUsers)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain HdUsers " + hdUsersListOldHdUsers + " since its coTipoUser field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<HdUsers> attachedHdUsersListNew = new ArrayList<HdUsers>();
            for (HdUsers hdUsersListNewHdUsersToAttach : hdUsersListNew) {
                hdUsersListNewHdUsersToAttach = em.getReference(hdUsersListNewHdUsersToAttach.getClass(), hdUsersListNewHdUsersToAttach.getCoUser());
                attachedHdUsersListNew.add(hdUsersListNewHdUsersToAttach);
            }
            hdUsersListNew = attachedHdUsersListNew;
            hdTipoUser.setHdUsersList(hdUsersListNew);
            hdTipoUser = em.merge(hdTipoUser);
            for (HdUsers hdUsersListNewHdUsers : hdUsersListNew) {
                if (!hdUsersListOld.contains(hdUsersListNewHdUsers)) {
                    HdTipoUser oldCoTipoUserOfHdUsersListNewHdUsers = hdUsersListNewHdUsers.getCoTipoUser();
                    hdUsersListNewHdUsers.setCoTipoUser(hdTipoUser);
                    hdUsersListNewHdUsers = em.merge(hdUsersListNewHdUsers);
                    if (oldCoTipoUserOfHdUsersListNewHdUsers != null && !oldCoTipoUserOfHdUsersListNewHdUsers.equals(hdTipoUser)) {
                        oldCoTipoUserOfHdUsersListNewHdUsers.getHdUsersList().remove(hdUsersListNewHdUsers);
                        oldCoTipoUserOfHdUsersListNewHdUsers = em.merge(oldCoTipoUserOfHdUsersListNewHdUsers);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = hdTipoUser.getCoTipoUser();
                if (findHdTipoUser(id) == null) {
                    throw new NonexistentEntityException("The hdTipoUser with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            HdTipoUser hdTipoUser;
            try {
                hdTipoUser = em.getReference(HdTipoUser.class, id);
                hdTipoUser.getCoTipoUser();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The hdTipoUser with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<HdUsers> hdUsersListOrphanCheck = hdTipoUser.getHdUsersList();
            for (HdUsers hdUsersListOrphanCheckHdUsers : hdUsersListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This HdTipoUser (" + hdTipoUser + ") cannot be destroyed since the HdUsers " + hdUsersListOrphanCheckHdUsers + " in its hdUsersList field has a non-nullable coTipoUser field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(hdTipoUser);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<HdTipoUser> findHdTipoUserEntities() {
        return findHdTipoUserEntities(true, -1, -1);
    }

    public List<HdTipoUser> findHdTipoUserEntities(int maxResults, int firstResult) {
        return findHdTipoUserEntities(false, maxResults, firstResult);
    }

    private List<HdTipoUser> findHdTipoUserEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(HdTipoUser.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public HdTipoUser findHdTipoUser(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(HdTipoUser.class, id);
        } finally {
            em.close();
        }
    }

    public int getHdTipoUserCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<HdTipoUser> rt = cq.from(HdTipoUser.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
